package com.ytt.core.api.controller.authority;

import com.ytt.core.model.authority.AuthorityDto;
import com.ytt.core.model.common.PageDataDTO;
import com.ytt.core.service.service.authority.AuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

/**
 * AuthorityController
 *
 * @author chenwen
 * @date 2019/9/16 16:49
 */
@Api(tags = {"权限管理"})
@RestController
@RequestMapping("/api/authorities")
@Validated
public class AuthorityController {
    private final AuthorityService authorityService;

    @Autowired
    public AuthorityController(AuthorityService authorityService) {
        this.authorityService = authorityService;
    }

    @ApiOperation(value = "根据ID查找权限")
    @GetMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('AUTHORITY_RETRIEVE')")
    public AuthorityDto findById(
            @ApiParam(value = "权限ID", required = true) @PathVariable String id) {
        return authorityService.findById(id);
    }

    @ApiOperation(value = "查找权限")
    @GetMapping(value = "")
    @PreAuthorize("hasAnyAuthority('AUTHORITY_RETRIEVE')")
    public PageDataDTO<AuthorityDto> findOnePage(
            @ApiParam(value = "页号，从0开始", defaultValue = "0") @RequestParam(value = "page", required = false) @Min(0) Integer page,
            @ApiParam(value = "每页纪录条数", defaultValue = "20") @RequestParam(value = "size", required = false) @Min(1) @Max(100) Integer size,
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(value = "权限所属的类型") @RequestParam(value = "type", required = false) String type,
            @ApiParam(value = "权限名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "权限编号") @RequestParam(value = "code", required = false) String code) {
        return authorityService.findOnePage(page, size, sort, type, name, code);
    }

    @ApiOperation(value = "查找所有权限")
    @GetMapping(value = "/all")
    @PreAuthorize("hasAnyAuthority('AUTHORITY_RETRIEVE')")
    public List<AuthorityDto> findAll() {
        return authorityService.findAll();
    }

  

    @ApiOperation(value = "类型名称下拉框")
    @GetMapping(value = "/types")
    public Set<String> checkAvailability(@ApiParam(value = "权限所属的类型") @RequestParam(value = "type", required = false) String type) {
        return authorityService.getAuthorityTypes(type);
    }

    @ApiOperation(value = "创建权限")
    @PostMapping(value = "")
    @PreAuthorize("hasAnyAuthority('AUTHORITY_CREATE')")
    public AuthorityDto create(@ApiParam(value = "权限信息", required = true) @RequestBody @Validated AuthorityDto authorityEditInfoDTO) {
        return authorityService.create(authorityEditInfoDTO);
    }

    @ApiOperation(value = "更新权限")
    @PutMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('AUTHORITY_UPDATE')")
    public AuthorityDto update(@ApiParam(value = "权限ID", required = true) @PathVariable String id,
                               @ApiParam(value = "权限信息", required = true) @RequestBody @Validated AuthorityDto authorityEditInfoDTO) {
        return authorityService.update(id, authorityEditInfoDTO);
    }

    @ApiOperation(value = "删除权限")
    @DeleteMapping(value = "/{id}")
    @PreAuthorize("hasAnyAuthority('AUTHORITY_DELETE')")
    public void delete(@ApiParam(value = "权限ID", required = true) @PathVariable String id) {
        authorityService.delete(id);
    }

//    @ApiOperation(value = "分配权限给角色")
//    @PutMapping(value = "/{id}/roles")
//    @PreAuthorize("hasAnyAuthority('AUTHORITY_UPDATE')")
//    public void grantRoles(@ApiParam(value = "权限ID", required = true) @PathVariable String id,
//                           @ApiParam(value = "角色ID数组", required = true) @NotNull @RequestBody SimpleDataDTO<String[]> roles) {
//        authorityService.grantRoles(id, roles.getData());
//    }

}
